from selenium import webdriver
import time
from selenium.webdriver.common.by import By


# 实例化浏览器驱动
driver = webdriver.Chrome(".\chromedriver.exe")
# 获取百度页面
driver.get("https://www.baidu.com")
# 浏览器最大化
driver.maximize_window()
time.sleep(2)

# # 直接定位
# # 查找百度输入框元素，找到后返回的是一个元素对象
# baidu_input_ele = driver.find_element_by_id("kw")
# # baidu_input_ele = driver.find_element_by_css_selector("#kw")
# # baidu_input_ele = driver.find_element_by_xpath("//input[@id='kw']")
#
# # 通过by定位
# # baidu_input_ele = driver.find_element(By.ID, "kw")
# # baidu_input_ele = driver.find_element(By.CSS_SELECTOR, "#kw")
# # baidu_input_ele = driver.find_element(By.XPATH, "//input[@id='kw']")
# # 百度输入框输入：坚果
# baidu_input_ele.send_keys("坚果")
# time.sleep(2)
# # 查找百度一下按钮元素
# baidu_button = driver.find_element(By.ID, "su")
# # 点击-百度一下
# baidu_button.click()

# # 查找一组元素，返回的是一个列表，列表中每个元素都是一个页面元素对象，可以对其执行元素操作
# title_eles = driver.find_elements(By.CSS_SELECTOR, "a[class^=mnav]")
# for ele in title_eles:
#     ele.click()
#     time.sleep(2)

# # 输入框输入：坚果
# driver.find_element(By.ID, 'kw').send_keys("坚果")
# time.sleep(2)
# # # 点击-百度一下
# # driver.find_element(By.ID, "su").click()
# # 提交表单
# form_ele = driver.find_element(By.CSS_SELECTOR, "form")
# form_ele.submit()
# time.sleep(2)
# # 输入框清空
# driver.find_element(By.ID, 'kw').clear()
# # 点击-百度一下

# 定位设置按钮元素
setting_ele = driver.find_element(By.ID, "s-usersetting-top")
print("设置标签文本值是：", setting_ele.text)
print("class属性值为：", setting_ele.get_attribute("class"))
print("id属性值为：", setting_ele.get_attribute("id"))
print("name属性值为：", setting_ele.get_attribute("name"))
print("当前元素尺寸为：", setting_ele.size)


time.sleep(5)
driver.quit()